package com.dolphin.common.component.var.model;

import com.dolphin.common.component.var.enums.VarTypeEnum;
import com.google.common.base.Preconditions;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Map;

/**
 * @author shida
 */
@Getter
@Setter
@ToString(callSuper = true)
public class ScriptVarModel extends ExtVarModel{
    private static final String CATEGORY = "SCRIPT";
    private String scriptType;
    private String scriptContent;
    private Map<String, String> mapping;

    @Override
    public String getBid() {
        return VarTypeEnum.EXT_SCRIPT_VAR.getIndex() + getId();
    }

    public Expression fetchAviatorExpression() {
        Preconditions.checkArgument(getScriptContent() != null, "script should not be null");
        return AviatorEvaluator.compile(getScriptContent(), true);
    }

    @Override
    public VarTypeEnum getVarType() {
        return VarTypeEnum.EXT_SCRIPT_VAR;
    }
}
